﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class UploadPosting : System.Web.UI.Page
{
    
    PostingDAO PostDAO = new PostingDAO();
    CompetitionDAO comDAO = new CompetitionDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            drlCompetition.DataSource = comDAO.SelectOngoing();
            drlCompetition.DataTextField = "CompetitionName";
            drlCompetition.DataValueField = "CompetitionID";
            drlCompetition.DataBind();
        }
  
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PostingDAO PostDAO = new PostingDAO();
        CompetitionDAO comDAO = new CompetitionDAO();
        try
        {
            //PostInfo post = new PostInfo();
            PostingInfo post = new PostingInfo();
            
            post.PostingName = txtPostName.Text;
            post.PostDate = DateTime.Now;
            post.Description = txtDescription.Text;

            if (txtPrice.Text != "")
                post.Price = double.Parse(txtPrice.Text);

            post.StudentId = Session["User"].ToString();
            //post.StudentId = "Std_001";
            post.Count = 0;
            post.Quotations = txtQuotation.Text;
            post.CompetitionId = Int32.Parse(drlCompetition.SelectedValue);
            //post.CompetitionId = 2;
       

        if (fuPainting.HasFile)
        {
            try
            {
                int intFileSizeLimit = 2000;
                string postedFile = fuPainting.PostedFile.FileName;
                string extension = System.IO.Path.GetExtension(postedFile);
                string fileName = System.IO.Path.GetFileName(postedFile);
                int size = fuPainting.PostedFile.ContentLength / 1024;
                extension = extension.ToLower();

               // if (fuPainting.PostedFile.ContentType == "image/jpeg")
                if (extension.Equals(".jpg") || extension.Equals(".gif"))
                {
                    if (size < intFileSizeLimit)
                    {
                        post.Url = fuPainting.FileName.ToString();
                        fuPainting.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
                        lblMessage.ForeColor = System.Drawing.Color.White;
                        lblMessage.Text = "<br /><br />" + "Uploaded file details <hr />" +
                            "File Name: " + fileName + "<br />" +
                            "File Extension Name: " + extension + "<br />" +
                            "File Size: " + size.ToString() + "KB";
                    }
                    else
                    {
                        lblMessage.Text = "File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
                    }
                }
                else
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = "Only image/jpeg file are allowed, try again!";
                    
                }



               

            }
            catch (Exception ex)
            {
                lblMessage.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

       // if (fuPainting.PostedFile.ContentType == "image/jpeg")
        if (System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".jpg") || System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".gif"))
        {
            int insert = PostDAO.InsertPosting(post);

            if (insert > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Upload Successfull!');", true);
            }
            else Label1.Text = "False";
        }

        }
        catch (Exception ex)
        {
            Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
     
 


    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

        txtDescription.Text = "";
        txtPostName.Text = "";
        txtPrice.Text = "";
        txtQuotation.Text = "";
        lblMessage.Text = "";
        Label1.Text = "";
      
      
    }
}